<?php 
	class CommonControl extends Control{

		public function assign_data(){
			
			$this->top_cate();
			$this->right_info();
			$this->eve_star();
			$this->his_star();
			$this->helper();
		}
		
		
		/**
		 * 首页数据
		 */
		public function  index_data(){
			
			return $data1;
		}
		/**
	 * 顶级分类
	 * @return [type] [description]
	 */
	public function top_cate(){
		//顶级分类
		$topCate = M('category')->where('pid=0')->all();
		$this->assign('topCate', $topCate);

		//累积提问
		// $askNum = M('ask')->count();
		// $this->assign('askNum', $askNum);
		$data=M('ask')->field('count(*) as c')->find();
			// var_dump($data);die;
			$data1=(int)$data["c"];
		$this->assign('datanum', $data1);

	}
	/**
	 * 右侧用户信息
	 */
	public function right_info(){
		

		if(isset($_SESSION['uid'])){
			$uid = (int)$_SESSION['uid'];
			$field = 'face,exp,point,answer,ask,accept,username';
			$userInfo = M('user')->where('uid='.$uid)->field($field)->find();
			// $userInfo=$userInfo[0];
			
			$userInfo['face'] = $this->face($userInfo);
			$userInfo['lv'] = $this->exp_to_level($userInfo);
			$userInfo['ratio'] = (int)$this->ratio($userInfo);

			$this->assign('userInfo', $userInfo);
		}
	}
	
	/**
	 * 本日回答最多的人
	 */
	public function eve_star(){

		$zero = strtotime(date('Y-m-d'));
		$field = 'face,count(*) as c,username,u.uid,exp,answer,u.accept';
		//查询时间大于今天0点并且对用户进行分组，前面统计 进行倒序排列 限制一个
		$eveStar=M('answer')->query("SELECT {$field} FROM hd_answer as an join hd_user as u ON an.uid=u.uid where an.time>{$zero} GROUP BY username order by c DESC LIMIT 1");
		if(isset($eveStar[0]))$eveStar=$eveStar[0];
		if(!empty($eveStar)){
			$eveStar['face'] = $this->face($eveStar);
			$eveStar['lv'] = $this->exp_to_level($eveStar);
			$eveStar['ratio'] = $this->ratio($eveStar);
		}

		$this->assign("eveStar", $eveStar);
	}

	/**
	 * 历史回答最多的人
	 */
	public function his_star(){
		// 定义查询内容
		$field = 'face,username,uid,exp,answer,accept';
		//对answer进行降序排列，找出最多的那个
		$hisStar=M('user')->field($field)->order('answer desc')->find();
		// 测试找出来的数据
		// var_dump($hisStar);die;
		if(!empty($hisStar)){
			$hisStar['face'] = $this->face($hisStar);
			$hisStar['lv'] = $this->exp_to_level($hisStar);
			$hisStar['ratio'] = $this->ratio($hisStar);
		}

		$this->assign('hisStar', $hisStar);

	}
		
	/**
	 * [helper 助人光荣榜]
	 * @return [type] [description]
	 */
	public function helper(){
		$field='uid,username,answer';
		$helper=M('user')->field($field)->limit(3)->order('answer desc')->all();
		// var_dump($helper);die;
		$this->assign('helper',$helper);

	}
	/**
	 * [get_cate 找到父级分类]
	 * @return [type] [description]
	 */
	
	  public function get_cate($cid){
	  	$arr=array();
	  	$data=M('category')->where('cid='.$cid)->find();
	  	if($data){
	  		$arr[]=$data;
	  		$arr=array_merge($this->get_cate($data['pid']),$arr);
	  	}
	  	return $arr;

	  }
	/**
	 * 经验转换等级
	 */
	public function exp_to_level($user){
		$exp = isset($user['exp'])?$user['exp']:0;
		for ($i = 0; $i< C('LV20')+1; $i++){
			if($exp<=C('LV' . $i)){
				return $i;
			}
		}

		if($exp > C('LV20')){
			return 20;
		}

	}
	/**
	 * 头像处理
	 */
	public function face($user){
		if(!empty($user['face'])){
			return $user['face'];
		}

		return __PUBLIC__ . '/Images/noface.gif';
	}
	/**
	 * 采纳率换算
	 */
	public function ratio($user){
		if(!empty($user) && isset($user['answer'])){
			
			// 这儿做个if判断避免遇见除数为0报错
			if( $user['answer']!=0){
				$num = $user['accept'] / $user['answer'];
				$ratio = (sprintf("%.2f", $num)) * 100;
			}else{
				$ratio=0;
			}
			
		} else {
			$ratio = 0;
		}

		return $ratio;
	}



	}



 ?>